package com.gy.education.mine;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.gy.education.BaseActivity;
import com.gy.education.R;
import com.gy.education.utils.ActivityUtil;
import com.gy.education.utils.AnimUtil;
import com.gy.education.utils.BitmapUtil;
import com.gy.education.utils.StringUtil;
import com.nostra13.universalimageloader.utils.L;

import java.io.File;
import java.io.IOException;

/**
 * Created by 高岳 on 2016/7/15.
 * Describe:学生认证
 */
public class IdentifyStudentActivity extends BaseActivity {

    private LinearLayout ll1,ll2,ll3;
    private ImageView img1,img2,img3;
    private String path;
    private int position;
    private Bitmap bitmapGk;

    @Override
    protected void initView() {
        setContentViewRes(R.layout.activity_indentify_student);
        setTitleMsg("学生认证");
        ll1 = (LinearLayout) findViewById(R.id.identify_student_ll1);
        ll2 = (LinearLayout) findViewById(R.id.identify_student_ll2);
        ll3 = (LinearLayout) findViewById(R.id.identify_student_ll3);
        img1 = (ImageView) findViewById(R.id.identify_student_img1);
        img2 = (ImageView) findViewById(R.id.identify_student_img2);
        img3 = (ImageView) findViewById(R.id.identify_student_img3);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void setListener() {
        super.setListener();
        ll1.setOnClickListener(this);
        ll2.setOnClickListener(this);
        ll3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        Intent intent;
        switch (v.getId()){
            case R.id.identify_student_ll1:
                intent = new Intent(mContext,IdentifyActivity.class);
                intent.putExtra("position",0);
                startActivityForResult(intent,1);
                AnimUtil.pushLeftInAndOut(this);
                break;
            case R.id.identify_student_ll2:
                intent = new Intent(mContext,IdentifyActivity.class);
                intent.putExtra("position",1);
                startActivityForResult(intent,1);
                AnimUtil.pushLeftInAndOut(this);
                break;
            case R.id.identify_student_ll3:
                intent = new Intent(mContext,IdentifyActivity.class);
                intent.putExtra("position",2);
                startActivityForResult(intent,1);
                AnimUtil.pushLeftInAndOut(this);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data == null)
            return;
        path = data.getStringExtra("path");
        position =data.getIntExtra("position",0);
        Bundle bundle = data.getExtras();
        setImageBitmap(bundle.getByteArray("bytes"));
    }


    /**
     * 将MainActivity传过来的图片显示在界面当中
     *
     * @param bytes
     */
    public void setImageBitmap(byte[] bytes) {
        Bitmap cameraBitmap = byte2Bitmap(bytes);
        // 根据拍摄的方向旋转图像（纵向拍摄时要需要将图像选择90度)
        Matrix matrix = new Matrix();
        matrix.setRotate(ActivityUtil.getPreviewDegree(this));
        cameraBitmap = Bitmap
                .createBitmap(cameraBitmap, 0, 0, cameraBitmap.getWidth(),
                        cameraBitmap.getHeight(), matrix, true);
        bitmapGk = cameraBitmap;
        if(position == 0){
            img1.setImageBitmap(cameraBitmap);
        } else  if(position == 1){
            img2.setImageBitmap(cameraBitmap);
        } else  if(position == 2){
            img3.setImageBitmap(cameraBitmap);
        }
    }

    /**
     * 从Bundle对象中获取数据
     *
     * @return
     */
    public byte[] getImageFormBundle() {
        Intent intent = getIntent();
        Bundle data = intent.getExtras();
        byte[] bytes = data.getByteArray("bytes");
        return bytes;
    }

    /**
     * 将字节数组的图形数据转换为Bitmap
     *
     * @return
     */
    private Bitmap byte2Bitmap() {
        byte[] data = getImageFormBundle();
        // 将byte数组转换成Bitmap对象
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        return bitmap;
    }

    /**
     * 将字节数组的图形数据转换为Bitmap
     *
     * @return
     */
    private Bitmap byte2Bitmap(byte[] data) {
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        return bitmap;
    }

    private void doPostUpload() {
        showLoadDialog();
        File file = new File(path);
        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inTempStorage = new byte[1024 * 100];
        options.inSampleSize = 4;
        int bitmapDegree = StringUtil.getBitmapDegree(file.getAbsolutePath());
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        Bitmap bitmap1 = StringUtil.rotateBitmapByDegree(bitmap, bitmapDegree);

        try {
            BitmapUtil.saveBitmap(bitmap1, file.getParent());
        } catch (IOException e) {
            e.printStackTrace();
        }
        File file1 = new File(file.getParent() + "/image.png");
        if (!file1.exists()) {
            L.i(file1.getPath() + "...");
            file1 = new File(path);
        }

    }
}
